package com.deanlib.cabinet.utils;

import android.text.TextUtils;

import com.deanlib.cabinet.data.model.Login;
import com.deanlib.cabinet.data.model.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.mmkv.MMKV;

import java.util.Map;

/**
 * 用户管理
 * Created by Dean on 2022/6/21
 */
public class UserManager {

    public static final String KEY_LOGIN_TOKEN = "LOGIN_TOKEN";
    public static final String KEY_LOGIN = "LOGIN";
    public static final String KEY_SHARED_USERS = "SHARED_USERS";
//    public static final String KEY_USER_INFO = "USER_INFO";

    private Gson gson;
    private static UserManager lm;
    private String token;
//    private Auth auth;
//    private User user;
    private Login login;
    private Map<Long, User> sharedUserMap;//共享数据的用户集
    private final Object lock = new Object();

    private UserManager(){
        gson = new Gson();
        token = MMKV.defaultMMKV().decodeString(KEY_LOGIN_TOKEN);
        login = MMKV.defaultMMKV().decodeParcelable(KEY_LOGIN, Login.class);
//        auth = MMKV.defaultMMKV().decodeParcelable(KEY_LOGIN_AUTH, Auth.class);
//        user = MMKV.defaultMMKV().decodeParcelable(KEY_USER_INFO, User.class);
//        System.out.println(login);
        String sharedUserString = MMKV.defaultMMKV().decodeString(KEY_SHARED_USERS);
        if (!TextUtils.isEmpty(sharedUserString)){
            sharedUserMap = gson.fromJson(sharedUserString, new TypeToken<Map<Long, User>>(){}.getType());
        }

    }

    public static UserManager getInstance(){
        if (lm == null){
           synchronized (UserManager.class){
               if (lm == null){
                   lm = new UserManager();
               }
           }
        }
        return lm;
    }

    public String getToken() {
        return token;
    }

    public Login getLogin() {
        return login;
    }

    public synchronized boolean save(Login login){
        boolean isSaved = false;
        if (login!=null && login.auth!=null && !TextUtils.isEmpty(login.auth.token)){
            token = login.auth.token;
            this.login = login;
            MMKV.defaultMMKV().encode(KEY_LOGIN_TOKEN, token);
            MMKV.defaultMMKV().encode(KEY_LOGIN, login);
//            if (login.user!=null){
//                user = login.user;
//                MMKV.defaultMMKV().encode(KEY_USER_INFO, login.user);
//            }
            isSaved = true;
        }

        return isSaved;
    }

    public boolean isLogin(){
        if (TextUtils.isEmpty(token)){
            synchronized (lock){
                if (TextUtils.isEmpty(token)){
                    token = MMKV.defaultMMKV().decodeString(KEY_LOGIN_TOKEN);
                    login = MMKV.defaultMMKV().decodeParcelable(KEY_LOGIN, Login.class);
                }
            }
        }
        return !TextUtils.isEmpty(token) && login!=null;//todo 并没有判断是否过期
    }

    public void exit(){
        MMKV.defaultMMKV().remove(KEY_LOGIN_TOKEN);
        MMKV.defaultMMKV().remove(KEY_LOGIN);
        MMKV.defaultMMKV().remove(KEY_SHARED_USERS);
        token = null;
        login = null;
        sharedUserMap = null;
    }

    public Map<Long, User> getSharedUserMap() {
        return sharedUserMap;
    }

    public void setSharedUserMap(Map<Long, User> sharedUserMap) {
        this.sharedUserMap = sharedUserMap;
        if (sharedUserMap !=null) {
            MMKV.defaultMMKV().encode(KEY_SHARED_USERS, gson.toJson(sharedUserMap));
        }else {
            MMKV.defaultMMKV().remove(KEY_SHARED_USERS);
        }
    }
}
